package com.cinc.mdm.modules.factory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.mdm.constant.MdmConstant;
import com.cinc.mdm.modules.bom.entity.MdmBom;
import com.cinc.mdm.modules.factory.entity.MdmFactory;
import com.cinc.mdm.modules.factory.mapper.MdmFactoryMapper;
import com.cinc.mdm.modules.factory.model.FactoryTreeModel;
import com.cinc.mdm.modules.factory.service.IMdmFactoryService;
import org.jeecg.common.system.vo.SysDepartModel;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 工厂定义
 * @Author: jeecg-boot
 * @Date:   2021-10-08
 * @Version: V1.0
 */
@Service
public class MdmFactoryServiceImpl extends ServiceImpl<MdmFactoryMapper, MdmFactory> implements IMdmFactoryService {

    /**
     * 公司部门工厂树查询
     *
     * @param name
     * @return java.util.List<com.cinc.mdm.modules.factory.model.FactoryTreeModel>
     * @throws
     */
    @Override
    public List<FactoryTreeModel> queryTreeList(String name) {
        List<FactoryTreeModel> treeList = new ArrayList<>();
        List<SysDepartModel> tempDepartList = baseMapper.selectDepartListByNameLike(name);
        if(oConvertUtils.listIsEmpty(tempDepartList)) {
            return treeList;
        }
        List<String> orgCodeList = new ArrayList<>();
        // 公司部门树
        for (SysDepartModel sysDepart : tempDepartList) {
            if(orgCodeList.contains(sysDepart.getOrgCode())){
                continue;
            }
            int orgTypeInt = Integer.parseInt(sysDepart.getOrgType());
            for(int i=0; i<orgTypeInt; i++){
                String superOrgCode = sysDepart.getOrgCode().substring(0, sysDepart.getOrgCode().length() - 3*i);
                if(orgCodeList.contains(superOrgCode)){
                    break;
                }
                orgCodeList.add(superOrgCode);
            }
        }
        List<SysDepartModel> sysDepartList = baseMapper.selectDepartListByOrgCode(orgCodeList);
        List<SysDepartModel> companyList = sysDepartList.stream().filter(sysDepartModel -> sysDepartModel.getOrgType().equals("1")).collect(Collectors.toList());
        for(SysDepartModel company : companyList){
            FactoryTreeModel treeModel = new FactoryTreeModel();
            treeModel.setId(company.getId());
            treeModel.setName(company.getDepartName());
            treeModel.setCode(company.getOrgCode());
            treeModel.setTel(company.getMobile());
            treeModel.setAddress(company.getAddress());
            treeModel.setRemark(company.getMemo());
            treeModel.setType(MdmConstant.CODE_TYPE01);
            setDeptChilren(treeModel,sysDepartList);
            treeList.add(treeModel);
        }
        // 添加工厂到树
        LambdaQueryWrapper<MdmFactory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(MdmFactory::getFactoryName, name);
        queryWrapper.orderByAsc(MdmFactory::getFactoryCode);
        List<MdmFactory> mdmFactories = baseMapper.selectList(queryWrapper);
        for(MdmFactory mdmFactory : mdmFactories){
            FactoryTreeModel company = treeList.stream().filter(treeModel -> treeModel.getId().equals(mdmFactory.getCompanyId())).findFirst().orElse(null);
            if(company == null){
                continue;
            }
            FactoryTreeModel treeModel = new FactoryTreeModel();
            treeModel.setId(mdmFactory.getId());
            treeModel.setName(mdmFactory.getFactoryName());
            treeModel.setType(MdmConstant.CODE_TYPE03);
            treeModel.setCode(mdmFactory.getFactoryCode());
            treeModel.setTel(mdmFactory.getTel());
            treeModel.setAddress(mdmFactory.getAddress());
            treeModel.setRemark(mdmFactory.getRemark());
            if(oConvertUtils.listIsEmpty(company.getChildren())){
                company.setChildren(new ArrayList<>());
            }
            company.getChildren().add(treeModel);
        }

        return treeList;
    }

    private void setDeptChilren(FactoryTreeModel treeModel, List<SysDepartModel> sysDepartList){
        List<SysDepartModel> departChilren = sysDepartList.stream()
                .filter(departModel -> departModel.getParentId().equals(treeModel.getId()))
                .sorted(Comparator.comparing(SysDepartModel::getOrgCode))
                .collect(Collectors.toList());
        if(oConvertUtils.listIsNotEmpty(departChilren)){
            List<FactoryTreeModel> chilren = new ArrayList<>();
            for(SysDepartModel departModel : departChilren){
                FactoryTreeModel child = new FactoryTreeModel();
                child.setId(departModel.getId());
                child.setName(departModel.getDepartName());
                child.setType(MdmConstant.CODE_TYPE02);
                treeModel.setCode(departModel.getOrgCode());
                treeModel.setTel(departModel.getMobile());
                treeModel.setAddress(departModel.getAddress());
                treeModel.setRemark(departModel.getMemo());
                chilren.add(child);
                setDeptChilren(child, sysDepartList);
            }
            treeModel.setChildren(chilren);
        }
    }
}
